#!/bin/bash
# ========================================
# ConfigTools - Maven Assembly Plugin 打包脚本
# 方案二：分离依赖打包
# ========================================

echo ""
echo "========================================"
echo "ConfigTools Maven Assembly Plugin 打包"
echo "========================================"
echo ""

echo "开始清理和打包..."
mvn clean package -q

if [ $? -ne 0 ]; then
    echo ""
    echo "[错误] 打包失败！请检查Maven配置和依赖。"
    exit 1
fi

echo ""
echo "========================================"
echo "打包成功！"
echo "========================================"
echo ""
echo "分发包位置: target/config-tools-1.0.0-app.zip"

# 获取文件大小
if [ -f "target/config-tools-1.0.0-app.zip" ]; then
    size=$(stat -c%s "target/config-tools-1.0.0-app.zip" 2>/dev/null || stat -f%z "target/config-tools-1.0.0-app.zip" 2>/dev/null)
    sizeMB=$((size / 1024 / 1024))
    echo "文件大小: 约 ${sizeMB} MB"
fi

echo ""
echo "分发包内容:"
echo "  - config-tools-1.0.0.jar     (应用程序JAR)"
echo "  - lib/                       (依赖库目录)"
echo "  - config/                    (配置文件目录)"
echo "  - start.bat / start.sh       (启动脚本)"
echo ""
echo "部署方式:"
echo "  1. 解压 config-tools-1.0.0-app.zip 到目标目录"
echo "  2. Windows: 双击 start.bat"
echo "  3. Linux/Mac: 执行 ./start.sh"
echo ""
echo "优势特点:"
echo "  - 依赖库分离，便于维护和更新"
echo "  - 配置文件外置，便于自定义"
echo "  - 启动脚本包含JVM优化参数"
echo "  - 适合企业级部署和生产环境"
echo ""

# 询问是否解压查看
read -p "是否解压查看分发包结构? (y/n): " extract
if [[ "$extract" =~ ^[Yy]$ ]]; then
    if [ -d "target/config-tools-1.0.0-app" ]; then
        rm -rf "target/config-tools-1.0.0-app"
    fi
    echo "正在解压..."
    unzip -q "target/config-tools-1.0.0-app.zip" -d "target/"
    echo ""
    echo "解压完成！目录结构:"
    if command -v tree >/dev/null 2>&1; then
        tree "target/config-tools-1.0.0-app"
    else
        find "target/config-tools-1.0.0-app" -type f | head -20
        echo "..."
    fi
    echo ""
    echo "可以进入 target/config-tools-1.0.0-app 目录测试运行"
fi

echo ""
